﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using RTSoft.Modules;
using System.Data;

namespace RTSoft.SoureCode.Control.Quyen
{
    class quyenctrl
    {
        DataProvider provider = new DataProvider();
        SqlCommand cmd = new SqlCommand();
        String strSQL = "";

        public void getData(ref String errConnect, ref String errSQL, ref DataTable tbl)
        {
            provider.Connection(ref errConnect);
            try
            {
                tbl = new DataTable();
                strSQL = "select * from QUYEN ";
                tbl = provider.LayDanhSach(strSQL);
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
            }
            finally
            {
                provider.Disconnect();
            }
        }

        public void Insert(ref String errConnect, ref String errSQL, quyeninfo info)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                SqlParameter f = new SqlParameter();
                f.IsNullable = true;
                f.Value = System.DBNull.Value;
                strSQL = "Insert into QUYEN";
                strSQL = strSQL + "(TEN_QUYEN,TRANGTHAI,NGAY_TAO)";
                strSQL = strSQL + " values";
                strSQL = strSQL + "(@TEN_QUYEN,@TRANGTHAI,@NGAY_TAO)";
                cmd.CommandText = strSQL;
                cmd.Parameters.Clear();
                cmd.Parameters.AddWithValue("@TEN_QUYEN", info.TEN_QUYEN);
                cmd.Parameters.AddWithValue("@TRANGTHAI", info.TRANGTHAI);
                cmd.Parameters.AddWithValue("@NGAY_TAO", info.NGAY_TAO);
                cmd.ExecuteNonQuery();
                provider.CommitTransaction();
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }

        }

        public void Update(ref String errConnect, ref String errSQL, quyeninfo info)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                SqlParameter f = new SqlParameter();
                f.IsNullable = true;
                f.Value = System.DBNull.Value;
                strSQL = "Update QUYEN SET  ";
                strSQL = strSQL + "TEN_QUYEN=@TEN_QUYEN,TRANGTHAI=@TRANGTHAI,NGAY_TAO=@NGAY_TAO Where  ID_QUYEN = @ID_QUYEN";
                cmd.CommandText = strSQL;

                cmd.Parameters.AddWithValue("@ID_QUYEN", info.ID_QUYEN);
                cmd.Parameters.AddWithValue("@TEN_QUYEN", info.TEN_QUYEN);
                cmd.Parameters.AddWithValue("@TRANGTHAI", info.TRANGTHAI);
                cmd.Parameters.AddWithValue("@NGAY_TAO", info.NGAY_TAO);
                cmd.ExecuteNonQuery();
                provider.CommitTransaction();
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }

        }

        public void Delete(ref String errConnect, ref String errSQL, int id)
        {
            cmd.Connection = provider.Connection(ref errConnect);
            if (errConnect != "")
            {
                return;
            }
            cmd.Transaction = provider.BeginTransaction();
            try
            {
                strSQL = "DELETE FROM QUYEN WHERE ID_QUYEN=@RowID";
                cmd.Parameters.AddWithValue("@RowID", id);
                cmd.CommandText = strSQL;
                cmd.ExecuteNonQuery();
                provider.CommitTransaction();
                //' Thuc hien transaction 
            }
            catch (Exception ex)
            {
                errSQL = ex.Message;
                provider.RollbackTransaction();
                //' Neu xay ra loi thi rollback lai
            }
            finally
            {
                //'Dong ket noi
                cmd.Connection.Close();
                cmd.Connection.Dispose();
            }
        }
    }
}
